/*
 * @Author: ylq
 * @Date: 2018-09-28 17:01:39
 * @Desc: store Action
 * @Last Modified by: ylq
 * @Last Modified time: 2022-08-10 18:03:54
 */
//import { clientService } from '@/api'
import storage from '@/utils/storage'
import CONFIG from '@/config/config'
import router from '@/router/'
import { asyncRouterMap } from '@/router/route'
import { tools } from '@/utils/tools'

console.log(15, asyncRouterMap())
// 搜索路由
function filterAsyncRouter(arm, roles) {
  console.log(18, arm, roles)
  let accessedRouters = arm.filter((route) => {
    let meta = route.meta
    //没有设置roleId默认都可以访问
    if (meta && !meta.roleId) {
      //没有设置roleId，但是有子菜单的
      if (meta.drop) {
        let children = route.children
        if (children && children.length) {
          route.children = filterAsyncRouter(children, roles)
          if (!route.redirect) {
            route.redirect = route.path + '/' + route.children[0].path
          }
        }
      }
      return true
    }
    // 有roleId的
    if (meta && roles.indexOf(meta.roleId) >= 0) {
      let children = route.children
      console.log(39, children)
      if (children && children.length) {
        route.children = filterAsyncRouter(children, roles)
        console.log(40, route, route.children)
        if (!route.redirect) {
          console.log(42, route.children, route.children[0])
          route.redirect = route.path + '/' + route.children[0].path
        }
      }
      return true
    }
    return false
  })
  return accessedRouters
}
// 退出删除用户状态
const delUserStatus = (commit) => {
  // 删除用户信息TOKEN
  storage.removeItem(CONFIG.TOKEN)
  tools.setCookie(CONFIG.COOKIE_NAME, 0, -1)
  // 删除时间
  // console.log(oldTime)
  storage.removeItem(CONFIG.LAST_LOGIN_TIME)
  // 清除 权限store
  commit('SET_ROLES', [])
  // 清除 用户信息
  commit('SET_USERINFO', null)
  // 清除 添加的路由
  commit('SET_ROUTERS', [])
  // 清除 accLists
  // commit('SET_TRACKACCLIST', [])
  // 跳转入登录页
  // return
  router.replace({ path: '/login' })
}

export default {
  // 获取用户信息
  GetUserInfo({ commit }) {
    // console.log(1,2,state)
    return new Promise((resolve) => {
      // clientService.getUserInfo()
      //   .then(response => {
      //     console.log('你的名字',response)
      //     if (!response.error || response.error) { //这里是登录不登录都可以进入，后续正式版本后端api完善后需要更改
      //       let result = {}
      //       let rule = ['home','dy-video','dy-live','data-center']

      //       // 开发者权限
      //       // if ((process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'dev')) {

      //       // }
      //       // 正式权限
      //       result.role = rule;
      //      console.log(148,rule,reject)
      //       // 权限信息
      //       commit('SET_ROLES', result.role)
      //       commit('SET_USERINFO', response.result)
      //       // commit('SET_DATAPER',per1000)
      //       // commit('SET_PERINFO', perInfo)
      //       resolve(result)
      //     } else {
      //       reject(response.error)
      //     }
      //   }).catch(error => {
      //     reject(error)
      //   })
      let result = {}
      let rule = ['home', 'dy-video', 'dy-live', 'data-center']

      result.role = rule
      // 权限信息
      commit('SET_ROLES', result.role)
      //commit('SET_USERINFO', response.result)
      // commit('SET_DATAPER',per1000)
      // commit('SET_PERINFO', perInfo)
      resolve(result)
    })
  },

  // 生成路由
  GenerateRoutes({ commit }, data) {
    return new Promise((resolve) => {
      const roles = data
      let accessedRouters = filterAsyncRouter(asyncRouterMap(), roles)
      commit('SET_ROUTERS', accessedRouters)
      resolve()
    })
  },
  // logout
  Logout({ commit }) {
    return new Promise(() => {
      delUserStatus(commit)
      // api.login.logout().then(() => {
      //   delUserStatus(commit)
      // }).catch(() => {
      //   delUserStatus(commit)
      // })
    })
  },

  // // 获取线索自定义字段
  // GetCluesAttrs({ commit }) {
  //   return new Promise((resolve, reject) => {
  //     api.clue.getClueAttrs({})
  //       .then(response => {
  //         console.log('获取线索自定义字段')
  //         if (!response.error) {
  //           let result = response.result;
  //           // 线索自定义字段
  //           commit('SET_CLUEATTRS', result)
  //           resolve(result)
  //         } else {
  //           reject(response.error)
  //         }
  //       }).catch(error => {
  //         reject(error)
  //       })
  //   })
  // },
}
